#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:    御风
# @Mail:      zhong.yufeng@foxmail.com
# @Time:      2022/4/8 23:13


import config
from sqlalchemy import create_engine, text

# 连接 mysql 的时候总是报 Warning: (1366, "Incorrect string value ...)
# 直接忽略掉 Warning
import warnings

warnings.filterwarnings("ignore")


class DataBase(object):

    def __init__(self):
        self.engine = create_engine(
                url=config.MYSQL_URL,
                echo=config.MYSQL_DEBUG,
                pool_size=config.MYSQL_POOL_SIZE,
                pool_recycle=config.MYSQL_POOL_RECYCLE,
                pool_pre_ping=config.MYSQL_POOL_PRE_PING
        )

    @property
    def tables(self) -> list:
        cursor = self.engine.execute("SHOW TABLES")
        result = cursor.fetchall()
        if not result:
            return []
        return [v[0] for v in result]

    def text(self, string):
        return text(string)


if __name__ == "__main__":
    db = DataBase()
    print(db.tables)
